package com.ky.course.controller;


import cn.hutool.core.convert.NumberWithFormat;
import com.ky.common.config.SystemConfig;
import com.ky.common.param.CommentAdd;
import com.ky.common.param.CourseCommentVo;
import com.ky.common.util.JwtUtil;
import com.ky.common.vo.R;
import com.ky.course.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zpa
 * @since 2024-06-22
 */
@RestController
@RequestMapping("/course/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @GetMapping("/courseId/{courseId}")
    public R getCommentList(@PathVariable("courseId") Integer courseId){
       List<CourseCommentVo> list = commentService.getCommentList(courseId);
       return R.ok(list);
    }

    @PostMapping("/submitComment")
    public R submitComment(HttpServletRequest request, @RequestBody CommentAdd commentAdd){
        String token = request.getHeader(SystemConfig.HEADER_TOKEN);
        if (token==null){
            return R.fail("未登录");
        }
        if(commentAdd==null){
            return R.fail("参数错误");
        }
        Integer id = ((NumberWithFormat) JwtUtil.parseToken(token, "id")).intValue();
        if(id ==null){
            return R.fail("非法的请求");
        }

        return commentService.submitComment(id,commentAdd);

    }
}

